package com.nothing.emp.service.impl;
import com.nothing.common.util.Page;
import com.nothing.emp.dao.EmpDao;
import com.nothing.emp.service.EmpService;
import com.nothing.emp.vo.EmpVo;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


//该注解代表当前类是业务委托接口对象
@Service
@Transactional
public class EmpServiceImpl implements EmpService {
    @Resource
    EmpDao empDao;


    @Override
    public List queryAll() {
        return empDao.queryAllByHql("from EmpVo");
    }

    @Override
    public List queryByPage(Page page) {
        return empDao.queryByPage("from EmpVo",page);
    }

    //带分页 + 带条件搜索
    @Override
    public List queryByPageAndParam(Page page, Map params) {
        return empDao.queryByQbc(page,params);
    }

    @Override
    public int queryCount() {
        return empDao.queryCount(EmpVo.class);
    }

    @Override
    public boolean delete(EmpVo empVo) {
        return empDao.delete(empVo);
    }


    //分页+条件筛选+多表关联
    @Override
    public List queryBySql(Page page, Map params) {
        String sql = "select e1.*,e2.ename as mgrName,d.dname as dname from emp e1 \n" +
                "left join emp e2 on e1.mgr = e2.empno \n" +
                "left join dept d on e1.deptno = d.deptno where 1 = 1 ";
        //判断是否限制经理姓名
        if(params.get("eName") != null && !"".equals(params.get("eName"))){
            sql += "and eName like '%" + params.get("eName").toString() + "%'";
        }

        //判断是否限制经理姓名
        if(params.get("job") != null && !"".equals(params.get("job"))){
            sql += "and job like '%" + params.get("job").toString() + "%'";
        }

        //判断是否限制经理姓名
        if(params.get("mgrName") != null && !"".equals(params.get("mgrName"))){
            sql += "and mgrName like '%" + params.get("mgrName").toString() + "%'";
        }

        //判断是否筛选部门名称
        if(params.get("dName") != null && !"".equals(params.get("dName"))){
            sql += "and dName like '%" + params.get("dName").toString() + "%'";
        }

        sql += "order by e1.empno desc";
        return empDao.queryBySql(sql,page);
    }

    @Override
    public boolean save(EmpVo empVo) {
        return empDao.save(empVo);
    }

    //根据ID查询员工信息
    @Override
    public EmpVo queryById(Integer id) {
        return (EmpVo) empDao.queryById(id,EmpVo.class);
    }


}
